layui.define(['jquery'],
    function (exports) {
        var $ = layui.jquery;
        var kaFile = new function () {
            this.open = function (myopt) {
                if (myopt == undefined) {
                    myopt = {};
                }
                var opt = {
                    title: "文件上传",
                    synopsis: "正在上传一个文件",
                    accept: 'file',
                    size: 204800,
                    number: 20,
                    multiple: false,
                    url: kaCtx.location() + '/common/upload',
                    mb: {
                        title: "",
                        url: ""
                    },
                    cb: function (res) {
                        console.log(res);
                    }
                }
                $.extend(opt, myopt);
                var uuid = layui.kaPublic.uuidStr();
                layui.layer.open({
                    type: 1, // page 层类型
                    area: ['450px', '450px'],
                    title: '<i class="icon Kaultraiconfont Kaultraicon-yunshangchuan" style="margin-top: 10px;text-align: center;width: 50px;height: 50px;line-height: 50px;font-size: 36px;float: left;padding-left: 3px;position: absolute;background-color: #f3f1ff;border-radius: 50%;color: #024fe1;"></i>',
                    offset: ['20%', '30%'],
                    shade: [0.6, '#FFF'], // 点击遮罩区域，关闭弹层
                    maxmin: false, // 允许全屏最小化
                    anim: 0, // 0-6 的动画形式，-1 不开启
                    content: `
                        <div style="padding-left: 15px;padding-right: 15px;">
                            <div style="font-size: 17px;font-weight: bold;margin-bottom: 5px;margin-top: 25px;line-height: 1.6;">` + opt.title + `</div>
                            <div style="color: #989898;margin-top: 5px;font-size: 12px;line-height: 1.6;">` + opt.synopsis + `</div>
                            <div style="margin-top: -20px;width: calc(100% - 30px);text-align: right;position: absolute;">
                                <a href="`+ opt.mb.url + `" target="_bank" style="margin-left: 5px;">` + opt.mb.title + `</a>
                            </div>
                            <div class="layui-upload-drag" style="display: block;margin-top: 18px;line-height: 1.6;" id="` + uuid + `">
                                <i class="layui-icon layui-icon-upload"></i> 
                                <div>点击上传，或将文件拖拽到此处</div>
                                <div class="divUploadRender"></div>
                                <div class="layui-hide" id="ID-upload-demo-preview">
                                    <hr> <img src="" alt="上传成功后渲染" style="max-width: 100%">
                                </div>
                            </div>
                            <div>
                                <a class="layui-btn layui-btn-sm" id="` + uuid + `_btn" style="position: absolute;bottom: 10px;right: 12px;">确定上传</a>
                            </div>
                        </div>
                    `,
                    success: function () {
                        var loading1UUid = "";
                        layui.upload.render({
                            elem: '#' + uuid,
                            auto: false,
                            accept: opt.accept,
                            size: opt.size,
                            multiple: opt.multiple,
                            unified: true,
                            number: opt.number,
                            headers: {
                                "Authorization": 'Bearer ' + layui.kaToken.get()
                            },
                            before: function (obj) { // obj 参数同 choose
                                var loadLayerIndex = layui.kaMsg.loading();
                                this.loadLayer = loadLayerIndex;
                            },
                            bindAction: '#' + uuid + '_btn',
                            url: kaCtx.location() + '/common/upload', // 实际使用时改成您自己的上传接口即可。
                            error: function (index, upload, res, xhr) {
                                layui.kaMsg.error("请求失败", JSON.stringify(res));
                            },
                            choose: function (obj) {
                                var files = obj.pushFile();
                                obj.preview(function (index, file, result) {
                                    $(".divUploadRender").css("display", "block");
                                    $(".divUploadRender").html(file.name);
                                });
                            },
                            done: function (res) {
                                var loadLayerIndex = this.loadLayer;
                                if (loadLayerIndex > 0) {
                                    layui.kaMsg.close(loadLayerIndex);
                                }
                                if (res.code == 200) {
                                    layer.closeAll();
                                    opt.cb(res);
                                } else {
                                    layui.kaMsg.error("请求失败", JSON.stringify(res));
                                }
                            }
                        });
                    }
                });
            }
            this.clear = function () { // 删除
            }
        };
        exports('kaFile', kaFile);
    }
)